package com.qingyun.video.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qingyun.model.common.dtos.R;
import com.qingyun.model.video.pojos.Sensitive;
import com.qingyun.model.video.pojos.Video;
import com.qingyun.video.mapper.SensitiveMapper;
import com.qingyun.video.service.AutoScanService;
import com.qingyun.video.service.VideoService;
import com.qingyun.video.service.PublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.common.SensitiveWordUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Slf4j
@Transactional
public class AutoScanServiceImpl implements AutoScanService {

    @Lazy
    @Resource
    private PublishService publishService;

    @Resource
    private VideoService videoService;
    /**
     * 文章审核
     * @param id
     */
    @Async //标明该方法是一个异步方法
    public void autoScanVideo(Long id) {
        //查询文章
        Video video = publishService.query().eq("id",id).one();
        if (video==null){
            throw new RuntimeException("WmNewsAutoScanServiceImpl-视频不存在");
        }

        if (video.getStatus().equals(Video.Status.SUBMIT.getCode())){

            //自管理敏感词
            boolean isSensitive=handleSensitiveScan(video.getTitle(),video);
            if (!isSensitive)return;
            //审核文本内容
            boolean isTextScan=handleTextScan(video.getTitle(),video);
            if (!isTextScan)return;
            //审核图片

            //审核成功
            R responseResult = videoService.saveVideo(video);
            if (!responseResult.getCode().equals(1)){
                throw new RuntimeException("AutoScanServiceImpl-视频审核保存APP端失败!");
            }
            video.setId((Long) responseResult.getData());
            updateVideo(video,(short)9,"审核成功");
        }


    }

    @Resource
    private SensitiveMapper sensitiveMapper;

    /**
     * 自管理的敏感词审核
     *
     * @return
     */
    public boolean handleSensitiveScan(String content, Video video) {
        boolean flag=true;
        //获取敏感词
        List<Sensitive> sensitives = sensitiveMapper.selectList(Wrappers.<Sensitive>lambdaQuery().select(Sensitive::getSensitives));
        List<String> sensitiveList = sensitives.stream().map(Sensitive::getSensitives).collect(Collectors.toList());
        //初始化敏感词
        SensitiveWordUtil.initMap(sensitiveList);

        //审核
        Map<String, Integer> map = SensitiveWordUtil.matchWords(content);
        if (map.size()>0){
            updateVideo(video,(short) 2,"当前视频标题中存在违规内容"+map);
            flag=false;
        }
        return flag;
    }
    private void updateVideo(Video video, short status, String reason) {
        video.setStatus(status);
        video.setReason(reason);
        publishService.updateById(video);
    }

    /**
     * 审核纯文本内容
     *
     * @return
     */
    public boolean handleTextScan(String title, Video video) {
        if ((title).length()==0){
            return true;
        }
        return true;
    }




}
